package util;

import common.consts.Consts;
import common.consts.MsgPush;
import domain.PushMessage;
import wrapper.push_message.*;

import javax.management.Notification;
import java.util.List;

/**
 * Created by chpengzh <cpz@youjianghr.com> on 2015/7/18.
 * 工厂类，create方法创建相应的模式的MessageEntity对象，用于发送信息;
 */
public class MessageFactory implements MsgPush {

    //构造创建一个MessageEntity对象
    public static MessageEntity create(PushMessage msg, String mode) {
        switch (mode) {
            case (Mode.BROADCAST):
                return createBroadcast(msg);
            case (Mode.UNICAST):
                return createUnicast(msg);
            default:
                return null;
        }
    }

    public static MessageEntity createListcast(List<PushMessage> msgs){
        MessageEntity entity = initiate(null);
        //TODO
        return entity;
    }

    public static MessageEntity createGroupcast(PushMessage msg,String alias){
        MessageEntity entity = initiate(null);
        //TODO
        return entity;
    }

    //Done.
    private static MessageEntity createUnicast(PushMessage msg) {
        MessageEntity entity = initiate(msg);
        entity.setDevice_tokens(getDeviceTockenByAccountId(msg.getReceiver()));
        entity.setType("unicast");
        return entity;
    }

    //Done.
    private static MessageEntity createBroadcast(PushMessage msg) {
        MessageEntity entity = initiate(msg);
        entity.setType("broadcast");
        return entity;
    }


    private static MessageEntity initiate(PushMessage msg) {
        MessageEntity entity = new MessageEntity();
        //body
        Body body = new Body();
        body.setTitle(msg.getSubject());
        body.setText(msg.getContent());
        body.setTicker(Consts.i18nKey.PUSH_MESSAGE_TICKSER);
        body.setAfter_open(Body.AfterOpen.GO_APP);
        //payload
        PayLoad payload = new PayLoad();
        payload.setDisplay_type(PayLoad.DisplayType.NOTIFICATION);
        payload.setBody(body);
        //Appkey
        entity.setAppkey(MsgPush.AppKey);
        //Timestamp
        entity.setTimestamp(Long.valueOf(msg.getCreateTime().getTime()).toString());
        //payload
        entity.setPayload(payload);
        return entity;
    }

    private static String getDeviceTockenByAccountId(long id) {
        return "AhZVYMbPcrHrV0ZyeH0TPYwa5GykZv6F6DyQkQfVBHc2";
    }
}
